package com.wm.service.imp;

import java.util.List;

import com.wm.bean.OrderMenu;
import com.wm.bean.OrderResMenu;
import com.wm.bean.Orders;
import com.wm.bean.Scriptorium;
import com.wm.comm.Util;
import com.wm.dao.OrderMenuDAO;
import com.wm.dao.OrderResMenuDAO;
import com.wm.dao.OrdersDAO;
import com.wm.dao.RestaurantDAO;
import com.wm.dao.ScriptoriumDAO;
import com.wm.service.OrdersService;
import com.wm.service.web.PageList;
import com.wm.service.web.Pages;

/**
 * @author yangbo
 *
 * 2013-1-21
 */
public class OrdersServiceImp implements OrdersService {

	private OrdersDAO ordersDAO;
	private OrderResMenuDAO orderResMenuDAO;
	private ScriptoriumDAO scriptoriumDAO;
	private RestaurantDAO restaurantDAO;
	
	public RestaurantDAO getRestaurantDAO() {
		return restaurantDAO;
	}
	public void setRestaurantDAO(RestaurantDAO restaurantDAO) {
		this.restaurantDAO = restaurantDAO;
	}
	public OrdersDAO getOrdersDAO() {
		return ordersDAO;
	}
	public void setOrdersDAO(OrdersDAO ordersDAO) {
		this.ordersDAO = ordersDAO;
	}
	public OrderResMenuDAO getOrderResMenuDAO() {
		return orderResMenuDAO;
	}
	public void setOrderResMenuDAO(OrderResMenuDAO orderResMenuDAO) {
		this.orderResMenuDAO = orderResMenuDAO;
	}
	public ScriptoriumDAO getScriptoriumDAO() {
		return scriptoriumDAO;
	}
	public void setScriptoriumDAO(ScriptoriumDAO scriptoriumDAO) {
		this.scriptoriumDAO = scriptoriumDAO;
	}
	
	
	public PageList findOrdersByState(int state,String orderby,int ascOrDesc,Pages pages){
		PageList pl = new PageList();
        if (pages.getTotalNum() == -1) {
            pages.setTotalNum(this.getOrdersDAO().getOrderNumByState(state));
        }
        pages.executeCount();
        if(pages.getTotalNum() == 0){
        	pl.setObjectList(null);
        	pl.setPages(pages);
        	return pl;
        }
        List l = this.getOrdersDAO().findOrdersByState(state, orderby, ascOrDesc, pages.getSpage(), pages.getPerPageNum());
        if(l == null || l.isEmpty()){
	   		 pl.setObjectList(null);
	   	     pl.setPages(pages);
	   		 return pl;
		}else{
			for(int i=0;i<l.size();i++){
				Orders o = (Orders) l.get(i);
				List ml = this.getOrderResMenuDAO().findOrderResMenuByOrderId(o.getId(), "id", 0);
				Scriptorium s = this.getScriptoriumDAO().findScriptoriumById(o.getScrId());
				o.setMenuList(ml);
				o.setScriptorium(s);
			}
			pl.setObjectList(l);
	        pl.setPages(pages);
	        return pl;
		}  
	}

	public Orders findOrdersByid(long id) {
		Orders o = this.getOrdersDAO().findOrdersByid(id);
		List ml = this.getOrderResMenuDAO().findOrderResMenuByOrderId(o.getId(), "id", 0);
		Scriptorium s = this.getScriptoriumDAO().findScriptoriumById(o.getScrId());
		o.setMenuList(ml);
		o.setScriptorium(s);
		return o;
	}

	public long getOrderNumByState(int state) {
		return this.getOrdersDAO().getOrderNumByState(state);
	}

	public void removeOrdersById(long id) {
		this.getOrdersDAO().removeOrdersById(id);
	}

	public Orders saveOrders(Orders orders) {
		return this.getOrdersDAO().saveOrders(orders);
	}

	public Orders updateOrders(Orders orders) {
		return this.getOrdersDAO().updateOrders(orders);
	}
	
	public PageList findOrdersByUserIdFindType(long userId,int findType,String orderby,int ascOrDesc,Pages pages){
		PageList pl = new PageList();
		if (pages.getTotalNum() == -1) {
			pages.setTotalNum(this.getOrdersDAO().getOrderNumByUserIdFindType(userId,findType));
		}
		pages.executeCount();
        if(pages.getTotalNum() == 0){
        	pl.setObjectList(null);
        	pl.setPages(pages);
        	return pl;
        }
        List l = this.getOrdersDAO().findOrdersByUserIdFindType(userId,findType,orderby, ascOrDesc, pages.getSpage(), pages.getPerPageNum());
        if(l == null || l.isEmpty()){
	   		 pl.setObjectList(null);
	   	     pl.setPages(pages);
	   		 return pl;
		}else{
			int totalCopies=0;
			long totalPriceShow=0;
			for(int i=0;i<l.size();i++){
				Orders o = (Orders)l.get(i);
				//查订单详细
				totalCopies=0;
				
				o.setMenuList(this.getOrderResMenuDAO().findOrderResMenuByOrderId(o.getId(), "id", 0));
				if(o.getMenuList()!=null&&o.getMenuList().size()>0){
					for(int j=0;j<o.getMenuList().size();j++){
						OrderResMenu om = (OrderResMenu)o.getMenuList().get(j);
						om.setPriceShow(Util.getPriceShow(om.getPrice()));
						om.setTotalPriceShow(Util.getPriceShow(om.getPrice()*om.getCopies()));
						totalCopies=totalCopies+om.getCopies();
						totalPriceShow=totalPriceShow+om.getPrice()*om.getCopies();
					}
				}
				o.setTotalCopies(totalCopies);
				o.setTotalPriceShow(Util.getPriceShow(totalPriceShow));
				//查饭店联系方式
				o.setRestaurant(this.getRestaurantDAO().findRestaurantById(o.getResId()));
			}
			pl.setObjectList(l);
	        pl.setPages(pages);
	        return pl;
		} 
	}
	 
	public Orders findOrdersByUserIdState(long userId,int state){
		Orders o = this.getOrdersDAO().findOrdersByUserIdState(userId, state);
		List ml = this.getOrderResMenuDAO().findOrderResMenuByOrderId(o.getId(), "id", 0);
		o.setMenuList(ml);
		return o;
	}
	
	/**
	 * 根据resId,time查询记录
	 * @param resId
	 * @param time
	 * @param orderby
	 * @param ascOrDesc
	 * @param firstResult
	 * @param maxResults
	 * @return
	 */
	public PageList findOrdersByResIdTime(long resId,int time,String orderby,int ascOrDesc,Pages pages){
		PageList pl = new PageList();
        if (pages.getTotalNum() == -1) {
            pages.setTotalNum(this.getOrdersDAO().getOrderNumByResIdTime(resId, time));
        }
        pages.executeCount();
        if(pages.getTotalNum() == 0){
        	pl.setObjectList(null);
        	pl.setPages(pages);
        	return pl;
        }
        List l = this.getOrdersDAO().findOrdersByResIdTime(resId, time, orderby, ascOrDesc, pages.getSpage(), pages.getPerPageNum());
        if(l == null || l.isEmpty()){
	   		 pl.setObjectList(null);
	   	     pl.setPages(pages);
	   		 return pl;
		}else{
			for(int i=0;i<l.size();i++){
				Orders o = (Orders) l.get(i);
				List ml = this.getOrderResMenuDAO().findOrderResMenuByOrderId(o.getId(), "id", 0);
				Scriptorium s = this.getScriptoriumDAO().findScriptoriumById(o.getScrId());
				o.setMenuList(ml);
				o.setScriptorium(s);
			}
			pl.setObjectList(l);
	        pl.setPages(pages);
	        return pl;
		}  
	}

}
